package org.jeecg.modules.hbjh.jsvo.validate;

import org.apache.commons.lang3.ObjectUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.math.BigDecimal;


public class DecimalLengthValidator implements ConstraintValidator<DecimalLength, Object> {

    int maxLenth = 0;
     
    public void initialize(DecimalLength money) {
        maxLenth = money.max();
    }  
   
    public boolean isValid(Object value, ConstraintValidatorContext arg1) {
        if (value == null|| ObjectUtils.isEmpty(value)){
            //为空不校验
            return true;
        }
        try{
            int scale = new BigDecimal(value.toString()).scale();
            return scale<=maxLenth;
        }catch (Exception e){
            return false;
        }
    }
   
}  